package com.yinbao.testservice;

import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

import com.yinbao.testservice.inter.ServiceAidl;

/**
 * Created by xing on 2016/11/1.
 */
public class YouService extends Service {

    private static String TAG = "YouService";
    private MyBinder mBinder;
    private MyServiceConn conn;


    /**
     * 它与bindService()对应，通过返回IBinder，来与service交流。如果你并不像绑定它，就直接返回null
     * @param intent
     * @return
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind事件:它与bindService()对应，通过返回IBinder");
        return mBinder;
    }


    /**
     * 当服务被创建时调用，只调用一次
     */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate事件:当服务被创建时调用，只调用一次");
        if (mBinder==null){
            mBinder=new MyBinder();
        }
        conn=new MyServiceConn();
    }

    /**
     * 它与startService()对应，当服务启动后调用。如果你重写了该方法，你就有责任自己去
     * 当任务结束以后，调用stopSelf()或者stopService()来停止服务。如果你是绑定的服务，就不需重新该方法了
     *  服务被启动
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand事件:它与startService()对应，当服务启动后调用");
        //连接MyService
        this.bindService(new Intent(this,MyService.class),conn,BIND_IMPORTANT);
        Notification notification=new Notification();
        notification.tickerText="正在运行";
        notification.flags=BIND_IMPORTANT;
        startForeground(startId,notification);
        stopForeground(true);
        stopSelf();
        return START_STICKY;//服务就会在资源紧张的时候被杀掉，然后在资源足够的时候再恢复
    }

    /**
     * 当服务不再被使用时需要销毁时调用，你应该在这里用来停止线程，注销监听器，广播
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy事件:当服务不再被使用时需要销毁时调用");
        unbindService(conn);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e(TAG, "onConfigurationChanged事件：旋转事件");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.e(TAG, "onLowMemory事件");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Log.e(TAG, "onTrimMemory事件"+level);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind事件");

        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.e(TAG, "onRebind事件");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Log.e(TAG, "onTaskRemoved事件，结束任务，释放内存");
    }


    /**
     * AIDL交互
     */
    private class MyBinder extends ServiceAidl.Stub{
        @Override
        public void getName() throws RemoteException {
            Log.e(TAG,"aidl消息");
        }
    }


    /**
     * 服务连接交互
     */
    private class MyServiceConn implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"连接My服务成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG,"断开My服务成功");
            //表示My服务给杀死，重新启动
            YouService.this.startService(new Intent(YouService.this,MyService.class));
            YouService.this.bindService(new Intent(YouService.this,MyService.class),conn,BIND_IMPORTANT);
        }
    }
}
